<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once 'eleontev/HTML/DatePicker.php';


//$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
//$mtime = filemtime($_SERVER['SCRIPT_FILENAME']);
//$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

//if ($if_modified_since == $gmdate_mod) {
    //header("HTTP/1.0 304 Not Modified");
    //exit;
//}

/*
Header("Cache-Control: must-revalidate");
$offset = 60 * 60 * 24 * 3;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
Header($ExpStr);
*/

header("Cache-control: private");

class KBClientView_search_form extends KBClientView_common
{

	var $search_in_range = array('search_in_range' => array('article', 
															'article_title',
															'article_keyword', 
															'attachment', 
															'article_id'),
								
								'search_in_range_file' => array('fileall', 
																'filedesc', 
																'filecontent', 
																'filename',
																'file_id')
						   );
	
	function &execute(&$manager) {
		
		$this->home_link = true;
		$this->meta_title = $this->msg['advanced_search_msg'];
		$this->category_nav_generate = false; // not to generate categories in navigation line
		
		$data = $this->getTop($manager, $this->msg['advanced_search_msg'], false);
		$data .= $this->getForm($manager);
		
		return $data;
	}
	
	function getSearchInRange($manager) {
		$r = ($this->view_id == 'files') ? 'search_in_range_file' : 'search_in_range';
		$range_msg = AppMsg::getMsgs('ranges_msg.ini', false, $r);
		foreach($this->search_in_range[$r] as $k => $v) {
			$range[$v] = $range_msg[$v];
		}
		
		if(!$manager->isExtra('file')) { 
			unset($range['attachment']); 
		}
		
		return $range;
	}

	
	function getForm($manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . 'search_form.html');
		$tpl->tplAssign('base_href', $this->conf['client_home_path']);
		
		
		$submit = (isset($_GET['period']));
		$range = &$manager->getCategorySelectRange($manager->categories);
		$range = $this->stripVars($range);
		
		// categories
		$select = new FormSelect();
		$select->select_tag = false;
		$select->setRange($range);
		
		$s = (isset($_GET['c'])) ? $_GET['c'] : $this->category_id;
		$tpl->tplAssign('category_select', $select->select($s));
		
		//period
		$select->setRange(AppMsg::getMsgs('ranges_msg.ini', false, 'search_period_range'));
		$tpl->tplAssign('period_select', $select->select(@$_GET['period']));		
		
		// search in
		$range = $this->getSearchInRange($manager);
		$select->setRange($range);
		$tpl->tplAssign('search_in_select', $select->select(@$_GET['in']));			
		
		// dates
		$picker = new DatePicker();
		$picker->setFormName('search_form');		// set form name
		$picker->setFormMethod($_GET);				// set form method
		$picker->setYearRange(date('Y')-1, date('Y'));
		$picker->css_class = 'colorInput';
		
		//$date = $picker->js();					// js function
		//$tpl->tplAssign('js_date_select', $date);
		
		$picker->setSelectName('from');
		//$picker->setDate($picker->toUnixDate($dates['from']));		
		$date = $picker->day();						// select tag with days
		$date .= $picker->month();					// select tag with mohth
		$date .= $picker->year();					// select tag with years
		$tpl->tplAssign('date_from_select', $date);
		
		$picker->setSelectName('to');	
		//$picker->setDate($picker->toUnixDate($dates['to']));	
		$date = $picker->day();						// select tag with days
		$date .= $picker->month();					// select tag with mohth
		$date .= $picker->year();					// select tag with years
		$tpl->tplAssign('date_till_select', $date);
		
		
		// checkboxes
		$s = ($submit) ? $this->getChecked(@$_GET['cp']) : 'checked';
		$tpl->tplAssign('cp_checked', $s);
		
		$f = (@$_GET['pv'] == 'u') ? 'pvu_checked' : 'pvp_checked';
		$tpl->tplAssign($f, 'checked');
		
		$tpl->tplAssign('qt_checked', $this->getChecked(@$_GET['qt']));
		

		if(!$this->controller->mod_rewrite) {
			$view_key = $this->controller->getRequestKey('view');
			$msg_key = $this->controller->getRequestKey('msg');
			$ar = ($this->view_id == 'files') ?  array($view_key => 'files', $msg_key => 'search')
			                                  :  array($view_key => 'search');
			$tpl->tplAssign('hidden', http_build_hidden($ar));			
		}	
		
		
		$tpl->tplAssign('action_link', $this->_getLink('all'));
		$tpl->tplAssign('cancel_link', $this->_getLink());
		$tpl->tplAssign('user_msg', $this->getErrors());
		
		$tpl->tplParse(array_merge($this->msg, $this->getFormData(), $_GET));
		return $tpl->tplPrint(1);
	}
	
	
	function getChecked($var) {
		return ($var) ? 'checked' : '';
	}
}
?>